#include <stdio.h>
#include "main.h"
#include "mem.h"
#include "uart.h"
#include "funcBlock.h"
#include "funcBlock_STROMSTOSSRELAIS.h"

struct fb_STROMSTOSSRELAIS
{
	uint8_t type;
	function_t func;
	uint8_t old_Tr;
	uint8_t Tr;
	uint8_t S;
	uint8_t R;
	uint8_t Q;
};

void function_fb_STROMSTOSSRELAIS(struct funcBlock_t *_fb)
{
	struct fb_STROMSTOSSRELAIS *fb = (struct fb_STROMSTOSSRELAIS*)_fb;

	if (getState(fb->R) == 1)
		setState(fb->Q, 0);
	else if (getState(fb->S) == 1)
		setState(fb->Q, 1);
	else 
	{
		// positive Flanke ?
		if ((getState(fb->Tr) != 0) && (fb->old_Tr == 0))
		{
			setState(fb->Q, !getState(fb->Q));
			fb->old_Tr = 1;
		}

		if (getState(fb->Tr) == 0)
			fb->old_Tr = 0;
	}
}

struct funcBlock_t* get_fb_STROMSTOSSRELAIS(uint8_t Tr, uint8_t S, uint8_t R, uint8_t Q)
{
	struct fb_STROMSTOSSRELAIS *fb = NULL;

	fb = (struct fb_STROMSTOSSRELAIS*)getMem(sizeof(struct fb_STROMSTOSSRELAIS));
	if (fb != NULL)
	{
		//uart_puts("STROMSTOSSRELAIS\n");
		fb->type = FB_TYPE_STROMSTOSSRELAIS;
		fb->func = function_fb_STROMSTOSSRELAIS;
		fb->old_Tr = 0;
		fb->Tr = Tr;
		fb->S = S;
		fb->R = R;
		fb->Q = Q;
	}
	return (struct funcBlock_t*)fb;
}
